package com.czkevin.guicontrolsapi.controls;

import org.bukkit.event.Event;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import com.czkevin.guicontrolsapi.HeadTools;
import com.czkevin.guicontrolsapi.ItemUtils;

public class LabelControl extends AbstractSingleItemControl {

    private static final ItemStack DEFAULT_ICON = HeadTools.HEAD_IRON_SPEECH_BUBBLE;

    private ItemStack display;
    private String label;
    private String descriptions;
    private boolean changed;

    public LabelControl(String label, String descriptions) {
        this.label = label;
        this.descriptions = descriptions;
    }

    public LabelControl(ItemStack display, String label, String descriptions) {
        this.display = display.clone();
        this.label = label;
        this.descriptions = descriptions;
    }

    public void setLabel(String label) {
        this.label = label;
        setDisplayItem(createDisplay());
        changed = true;
    }

    public void setDescriptions(String descriptions) {
        this.descriptions = descriptions;
        setDisplayItem(createDisplay());
        changed = true;
    }

    @Override
    public ItemStack createDisplay() {
        ItemStack s = new ItemStack(display == null ? DEFAULT_ICON : display);
        ItemUtils.putMeta(s, false, label, descriptions.split("\n"));
        return s;
    }

    @Override
    public void onRender(boolean force) {
        if(!force && !changed) return;
        getWindow().setDisplayItemAt(getX(), getY(), getDisplayItem());
    }

    @Override
    public void onClick(InventoryClickEvent event, int localX, int localY) {
        event.setCancelled(true);
        event.setResult(Event.Result.DENY);
    }

    @Override
    public int getWidth() {
        return 1;
    }

    @Override
    public int getHeight() {
        return 1;
    }
}
